$(document).ready(function(){
	displayLang = $("#display-lang").val();
	
	$(".ftext").focus(function(){
		$(this).css("border-color","#256EB1");
		$(this).parent().find(".error-msg").hide();
		$(this).parent().find(".tips").show();
	});
	$("#gname").blur(function(){
		$(this).parent().find(".tips").hide();
		checkName(this);
	});
});
function validate(){
	var res= checkName($("#gname"));
	if(!res)
		return;
	var gname = $("#gname").val();
	
	var gid = createGroup(gname);
	
	 var dialog= $("<div><div class='ask'></div></div>");
	 $("body").append(dialog);
	 if(displayLang=="en"){
		 dialog.find(".ask").html("Go to invite group members now?");
	 }else if(displayLang=="zh"){
		 dialog.find(".ask").html("现在是否去邀请组成员 ?");
	 }
	var dialog_title;
	if (displayLang == "en") {
			dialog_title = "Info";
	} else if (displayLang == "zh") {
			dialog_title = "提示";
	}
	var yesLabel;
	if(displayLang=="en"){
		yesLabel ="Yes";
	}else if(displayLang=="zh")
		yesLabel = "是";
	var noLabel;
	if(displayLang=="en"){
		noLabel ="NO";
	}else if(displayLang=="zh")
		noLabel = "否";
	dialog.dialog({
		title : dialog_title,
		modal : true,
		width : 310,
		height:180,
		buttons : [
					{
						text :yesLabel,
						click : function() {
							dialog.dialog('close');
							 var from = $("#from");
							 var location;
							 if(from.length>0){
								   var f = from.val();
								   if(displayLang=="en")
								      location = "/sview/group/addmem.jsp?gid="+gid+"&from="+f;
								   else 
									   location = "/sview/group/addmem_zh.jsp?gid="+gid+"&from="+f; 
							 }else{
								   //go to group management
								   var f ;
								   if(displayLang=="en"){
									   f= "/sview/group/gmanage.jsp";
									   location = "/sview/group/addmem.jsp?gid="+gid+"&from="+f;
							       }
								   else if(displayLang=="zh"){
									   f= "/sview/group/gmanage_zh.jsp";
									   location = "/sview/group/addmem_zh.jsp?gid="+gid+"&from="+f;
								   }
							 }
							 window.location = location;
						}
					},
					{
						text : noLabel,
						click : function() {
							dialog.dialog('close');
							var from = $("#from");
							var location;
							 if(from.length>0){
								   var f = from.val();
								   location = f;
							 }else{
								   //go to group management
								   var f ;
								   if(displayLang=="en"){
									   location= "/sview/group/gmanage.jsp";
							       }
								   else if(displayLang=="zh"){
									   location= "/sview/group/gmanage_zh.jsp";
								   }
							 }
							 window.location = location;
					}
		            } ]
	});
}
function checkName(e){
	var flag = true;
//	var e = $("#gname");
	var value = $(e).val();
	if(value == ""){
		var msg = $(e).parent().find(".error-msg");
		var textEn = "Please enter a group name";
		var textZhSimple = "请输入组名";
		if(displayLang == "en"){
			msg.text(textEn);
		}else if(displayLang == "zh"){
			msg.text(textZhSimple);
		}
		msg.show();
		$(e).css("border-color","red");
		flag = false;
	}else{
		var reg = /^[A-Za-z0-9@._]{3,100}$/;
		if(reg.test(value)){
			flag =!checkExistGName(e);
		}else{
			var msg = $(e).parent().find(".error-msg");
			var textEn = "Wrong format of name";
			var textZhSimple = "组名格式错误";
			if(displayLang == "en"){
				msg.text(textEn);
			}else if(displayLang == "zh"){
				msg.text(textZhSimple);
			}
			msg.show();
			$(e).css("border-color","red");
			flag = false;
		}
	}
	return flag;
}
function checkExistGName(e){
	var flag = false;
	var value = $(e).val();
	var data = {};
	data["action"] = "checkExistGName";
	data["value"] = value;
	data = JSON.stringify(data);
	data = ("gcreate="+encodeURIComponent(data));
	
	$.ajax({
		url: "/sview/gcreateValidate",
		async:false,
		data: data,
		dataType: "text",
		type: "post",
		success: function(text){
			if(text=="exist"){
				flag = true;
				var msg = $(e).parent().find(".error-msg");
				var textEn = "This group name has existed. Please try another one.";
				var textZhSimple = "该组名已存在，请使用它其他组名";
				if(displayLang == "en"){
					msg.text(textEn);
				}else if(displayLang == "zh"){
					msg.text(textZhSimple);
				}
				msg.show();
				$(e).css("border-color","red");
			}else{
				flag = false;
			}
		}
	});
	return flag;
}
function search(){
	var value = $("#mname").val();
	var data={};
	data["action"] = "searchMember";
	data["value"] = value;
	data = JSON.stringify(data);
	data = ("gcreate="+encodeURIComponent(data));
	$.ajax({
		url:"/sview/gcreateValidate",
		asynic: false,
		data: data,
		dataType: "text",
		type: "post",
		success: function(text){
			if(text != null && text != "null"){
				//text = text.replace(/\n/g,"<br />");
				//text = text.replace(/\t/g,"&nbsp;&nbsp;");
				/*$(".searchResult ul li:gt(0)").remove();
				$(".searchResult ul").append(text);
				$(".searchResult ul div").addClass("labfirst");
				$(".searchResult ul li:last label:first").addClass("labsecond");
				$(".searchResult ul li:last label:last").addClass("labthird");*/
//				alert($(".searchResult tbody").length);
				var uid = text.split("\t")[0]
				var name = text.split("\t")[1];
				var email = text.split("\t")[2];
				$(".searchResult tbody").children().remove();
				$(".searchResult tbody").append("<tr id="+uid+"><td><input type='checkbox' checked='checked' /></td><td>"+name+"</td><td>"+email+"</td></tr>");
				$(".searchResult tbody tr td:eq(0)").addClass("labfirst");
				$(".searchResult tbody tr td:eq(1)").addClass("labsecond");
				$(".searchResult tbody tr td:eq(2)").addClass("labthird");
				
				//$(".searchResult").val().replace(/\n/g,"<br />").replace(/\t/g,"&nbsp;&nbsp;");
			}else{
				/*$(".searchResult ul li:gt(0)").remove();
				$(".searchResult ul").append("<li>no result returned</li>");*/
				$(".searchResult tbody").children().remove();
				$(".searchResult tbody").append("<tr><td colspan='3'>no result returned</td></tr>");
			}
		}
	});
}
function addMem(){
	var sli=$(".searchResult :checked").parent().parent();
	var mli = $(".selectResult tbody tr");/*$(".selectResult ul li");*/
	$(sli).each(function(){
		var id=$(this).attr("id");
		var selected = false;
		if(mli.length>0){
			$(mli).each(function(){
				if(id == $(this).attr("id")){
					selected = true;
					var uname = $(this).children("td:eq(1)");
					var displayLang = $("#display-lang").val();
					if(displayLang == 'en'){
						alert("user '"+uname.text()+"' already selected!");
					}else if(displayLang == 'zh'){
						alert("用户 '"+uname.text()+"' 已选择!");
					}
					return;
				}
			});
		}
		if(!selected){
			/*$(".selectResult ul").append($(this));*/
			$(".selectResult tbody").append($(this));
		}else{
		}
	});
}

function createGroup(groupName){
	var result ;
	var data={};
	data["uname"] = getUser();
	data["gname"] = groupName;
    data =  JSON.stringify(data);
	 data=("cg="+encodeURIComponent(data));
	 $.ajax({
		  url:WEBAPP+"gcreator",
		  async:false,
		  data:data,
		  dataType:"text",
		  type:"post",
		  success:function(data){
			  result = data;
		  }
    });
	 
	 return result;
}


function removeMem(){
	var mli = $(".selectResult :checked").parent().parent();
	$(".searchtext").val("");
	/*$(".searchResult ul").append($(mli));*/
	$(".searchResult tbody").append($(mli));
}
function addAll(){
	var displayLang = $("#display-lang").val();
	var mli = $(".selectResult :checked").parent().parent();
	if(mli.length==0){
		if(displayLang == 'en'){
			alert("No member added!");
		}else if(displayLang == 'zh'){
			alert("未添加任何成员");
		}
		return;
	}
	var mids="";
	$(mli).each(function(){
		mids += $(this).attr("id")+",";
	});
	var data={};
	data["action"] = "addAll";
	data["value"] = mids;
	data["gid"] = $("#gid").val();
	data = JSON.stringify(data);
	data = ("gcreate="+encodeURIComponent(data));
	$.ajax({
		url:"/sview/gcreateValidate",
		async: false,
		data: data,
		dataType: "text",
		type: "post",
		success: function(text){
			var displayLang = $("#display-lang").val();
			var from = $("#from");
			var location;
			if(from.length>0){
				 var f = from.val();
				 location = f;
		  }else{
				 var gid=$("#gid").val();
				 if(displayLang=="en"){
					 location = "/sview/group/gdetail.jsp?gid="+gid;
				 }
				 else if(displayLang=="zh"){
					 location = "/sview/group/gdetail_zh.jsp?gid="+gid; 
				 }
			}
			if(text!="null" && text!=null){
				alert(text);
				window.location = location;
			}else{
				if(displayLang == 'en'){
					alert("failed! please try latter");
				}else if(displayLang == 'zh'){
					alert("操作失败，请稍后再试");
				}
				
			}
		}
	});
}
/*function search(){
	var value = $("#mname").val();
	var data={};
	data["action"] = "searchMember";
	data["value"] = value;
	data = JSON.stringify(data);
	data = ("gcreate="+encodeURIComponent(data));
	$.ajax({
		url:"gcreateValidate",
		asynic: false,
		data: data,
		dataType: "text",
		type: "post",
		success: function(text){
			if(text != null){
				text = "name\temail\n"+text;
				//text = text.replace(/\n/g,"<br />");
				//text = text.replace(/\t/g,"&nbsp;&nbsp;");
				$(".searchResult").val(text);
				$(".searchResult").val().replace(/\n/g,"<br />").replace(/\t/g,"&nbsp;&nbsp;");
			}else{
				$(".searchResult").append("no result returned");
			}
		}
	})
	
}*/